//
//  SearchController.m
//  ServiceBaZhou
//
//  Created by 刘江涛 on 2017/8/22.
//  Copyright © 2017年 weierda. All rights reserved.
//

#import "SearchController.h"

#import "LFSearchCell.h"

#import "HXSearchBar.h"

#import "LFInforBaseController.h"

#import "CompanyModel.h"

@interface SearchController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

//数据源
@property (nonatomic,strong) NSMutableArray *dataArray;

//tableView
@property (nonatomic,strong) UITableView *tableView;

//搜索框
@property (nonatomic,strong) HXSearchBar *searchBar;

@end

@implementation SearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableView];
    
    CFLog(@"%@",_searchText);
    
    [self loadData:NO];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self.searchBar becomeFirstResponder];
}

- (void)loadData:(BOOL)isSearch{
    
    NSMutableDictionary *para = [NSMutableDictionary dictionaryWithCapacity:0];
    [para setObject:Token forKey:@"key"];
    if(!isSearch){
        [para setObject:_searchText forKey:@"keyword"];
    }else{
        [para setObject:_searchBar.searchBarTextField.text forKey:@"keyword"];
    }
    [para setObject:@"20" forKey:@"pageSize"];
    
    [CFHttpTools getRequest:@"ECIV4/Search" params:para success:^(id responseObj) {
        //
        
        if(![responseObj[@"Result"] isKindOfClass:[NSArray class]]){
            
            if(![responseObj[@"Result"] isKindOfClass:[NSArray class]]){
                if([CFToolUtil isBlankString:responseObj[@"Result"]]){
                    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 64, Width - 100 , Width - 100)];
                    imageView.image = [UIImage imageNamed:@"nodata"];
                    [self.tableView addSubview:imageView];
                    
                    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, Width - 20, Width, 20)];
                    label.text = @"暂无数据";
                    label.textColor = [UIColor grayColor];
                    label.textAlignment = NSTextAlignmentCenter;
                    [self.tableView addSubview:label];
                }
            }
        }else{
            
            NSArray *companyArray = responseObj[@"Result"];
            
            JSONModelArray *modelArray = [[JSONModelArray alloc]initWithArray:companyArray modelClass:[CompanyModel class]];
            
            self.dataArray = [NSMutableArray array];
            
            [self.dataArray addObjectsFromArray:(NSArray *)modelArray];
            
            [_tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        //
    }];
    
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height - 64 ) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = NO;
    
    [_tableView registerNib:[UINib nibWithNibName:@"LFSearchCell" bundle:nil] forCellReuseIdentifier:@"LFSearchCell"];
    
    [self.view addSubview:_tableView];
    
    self.searchBar = [[HXSearchBar alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 35)];
    self.searchBar.backgroundColor = [UIColor colorWithHexString:@"#168ec8"];
    self.searchBar.delegate = self;
    //输入框提示
    self.searchBar.placeholder = @"请输入企业名、人名、品牌、地址等关键字";
    //TextField
    self.searchBar.searchBarTextField.layer.cornerRadius = 4;
    self.searchBar.searchBarTextField.backgroundColor = [UIColor whiteColor];
    
    //去掉取消按钮灰色背景
    self.searchBar.hideSearchBarBackgroundImage = YES;
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, Width, 44)];
    headView.backgroundColor = [UIColor colorWithHexString:@"#168ec8"];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 35, 44);
    [backButton setImage:[UIImage imageNamed:@"homeReturen"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(12, 15, 12, 10);
    [headView addSubview:backButton];
    
    self.searchBar.frame = CGRectMake(35, 0, Width - 35, 44);
    [headView addSubview:self.searchBar];
    
    [self.view addSubview:headView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 98;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LFSearchCell *searchCell = [tableView dequeueReusableCellWithIdentifier:@"LFSearchCell"];
    
    CompanyModel *model = _dataArray[indexPath.row];
    searchCell.companyLabel.text = model.Name;
    searchCell.companyStatusLabel.text = [model.Status substringToIndex:2];
    searchCell.bossLabel.text = model.OperName;
    searchCell.moneyLabel.text = model.No;
    searchCell.timeLabel.text = [model.StartDate substringToIndex:10];
    return searchCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CompanyModel *model = _dataArray[indexPath.row];
    
    LFInforBaseController *infor = [[LFInforBaseController alloc]init];
    
    infor.model = model;
    
    [self.navigationController pushViewController:infor animated:YES];
}

#pragma mark - UISearchBar Delegate
//已经开始编辑时的回调
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    HXSearchBar *sear = (HXSearchBar *)searchBar;
    //取消按钮
    sear.cancleButton.backgroundColor = [UIColor clearColor];
    [sear.cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [sear.cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sear.cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
}

- (void)backClick:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [_searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self loadData:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
